package cn.lx.resource;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.io.*;
import java.net.URLEncoder;


/** 测试下载文件
 * Created by lxliuxuan on 2017/2/7.
 */
@Path("/signgS")
public class SignResourceT {
    @Context
    UriInfo uriInfo;
    @Context
    HttpHeaders httpHeaders;
    @Context
    SecurityContext sc;
    @Context
    Request req;

    @Context
    HttpServletResponse response;
    @Context
    HttpServletRequest request;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getAll(@QueryParam("filePath") String filePath){

        File file = new File(filePath);

        //如果文件不存在，提示404
        if(!file.exists()){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        String fileName = null;
        try {
            fileName = URLEncoder.encode("下载测试.xls", "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        return Response
                .ok(file)
                .header("Content-disposition","attachment;filename=" +fileName)
                .header("Cache-Control", "no-cache").build();
    }

    @GET
    @Path("/getByStream")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getAllByStream(@QueryParam("filePath") String filePath){

        FileStream fileStream = new FileStream("d:/test/poi/userSign.xls");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("下载测试.xls", "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        return Response
                .ok(fileStream)
                .header("Content-disposition","attachment;filename=" +fileName)
                .header("Cache-Control", "no-cache").build();
    }
    class FileStream implements StreamingOutput{
        private  String filePath;
        public FileStream(String filePath) {
            this.filePath = filePath;
        }

        public void write(OutputStream output) throws IOException, WebApplicationException {
            File file = new File(filePath);

            //如果文件不存在，提示404
            if(!file.exists()){
               throw  new WebApplicationException(404);
            }

            //将信息写入输入流
            output  = new FileOutputStream(file);
            output.flush();
            output.close();
        }
    }
    @GET
    @Path("/getByStream1")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getAllByStream1(@QueryParam("filePath") String filePath){

        FileStream fileStream = new FileStream("d:/test/poi/userSign.xls");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("下载测试.xls", "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        return Response
                .ok(fileStream)
                .header("Content-disposition","attachment;filename=" +fileName)
                .header("Cache-Control", "no-cache").build();
    }
}



